<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\model\Supervision as ModelSupervision;

/**
 * 会员督学控制器
 */
class Supervision extends Api
{
    protected $noNeedLogin = [''];
    protected $noNeedRight = ['*'];


    /**
     * 检测是否弹窗
     * @return void
     */
    public function checkPopup()
    {
        $user_id = $this->auth->id;
        $user_supervisions = (new ModelSupervision())->getUserValidSupervision($user_id);
        if ($user_supervisions && count($user_supervisions) > 0) {
            $need_popup  = false;
        } else {
            $need_popup  = true;
        }
        $this->success("OK", [
            'need_popup' => $need_popup,
            'data' => ['supervision_pic' => config('site.supervision_pic'), 'supervision_con' => config('site.supervision_con')]
        ]);
    }

    /**
     * 我的督学列表
     * @return void
     */
    public function list()
    {
        $user_id  = $this->auth->id;
        $type = $this->request->param('type'); #1=待批改,2=已批改
        if (!in_array($type, [1, 2])) {
            $this->error("参数错误");
        }
        $list = (new ModelSupervision)->getSupervisionList($user_id, $type);
        $this->success('督学列表', $list);
    }

    /**
     * 督学详情
     * @return void
     */
    public function detail()
    {
        # 督学id
        $supervision_id = $this->request->param('supervision_id');
        $user_id  = $this->auth->id;
        $info = (new ModelSupervision)->where('id', $supervision_id)->where('user_id', $user_id)->find();
        if (!$info) {
            $this->error("参数错误");
        }
        $info['is_end'] = $info['end_time'] > time() ? 1 : 0; #1=未截止,0=已截止
        # 判断是否批改
        if ($info['teacher_content'] != '' && $info['teacher_pic'] != '') {
            $info['is_correct'] = 1; #1=已批改,0=待批改
        } else {
            $info['is_correct'] = 0; #1=已批改,0=待批改
        }
        $info['supervision_pic'] = !empty($info['supervision_pic']) ? explode(',', $info['supervision_pic']) : [];
        $info['student_pic'] = !empty($info['student_pic']) ? explode(',', $info['student_pic']) : [];
        $info['teacher_pic'] = !empty($info['teacher_pic']) ? explode(',', $info['teacher_pic']) : [];
       
        $this->success('督学详情', $info);
    }

    /**
     * 学生回答
     * @param mixed $id
     * @return void
     */
    public function studentAnswers(){
        $supervision_id = $this->request->post('supervision_id');
        $student_content = $this->request->post('student_content');
        $student_pic = $this->request->post('student_pic');
        if(!$student_content){
            $this->error('请填写内容');
        }
        $info = (new ModelSupervision)->where('id', $supervision_id)->find();
        if (!$info) {
            $this->error('督学不存在');
        }
        $info->student_content = $student_content;
        $info->student_pic = $student_pic;
        $info->updatetime = time();
        $info->save();
        $this->success('提交成功');
    }

    
}
